#ifndef _BASE_HPP_20120407
#define _BASE_HPP_20120407

#include <assert.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <malloc.h>
#include <fcntl.h>
#include <stddef.h>

#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/epoll.h>

#include <iostream>
#include <sstream>
#include <algorithm>
#include <functional>

#include <string>

using namespace std;
using namespace __gnu_cxx;

extern int errno;

#define SAFE_DELETE(ptr) \
	if (ptr) \
	{ \
		delete ptr; \
		ptr  = 0; \
	}

#define SAFE_DELETE_ARR(arr) \
	if (arr) \
	{ \
		delete [] arr; \
		arr = 0; \
	}

#define SAFE_DELETE_VEC SAFE_DELETE_ARR

#define MIN(a, b) ((a) > (b)? (b): (a))

#define MAX(a, b) ((a) > (b)? (a): (b))

//// ------------ byte -----------------------

#define make_int16(b1, b0) \
	        ((((int16_t)b1) << 8) | ((int16_t)b0))

#define make_int32(b3, b2, b1, b0) \
	        ((((int32_t)make_int16(b3, b2)) << 16) | ((int32_t)make_int16(b1, b0)))

#define make_int64(b7, b6, b5, b4, b3, b2, b1, b0) \
	        ((((int64_t)make_int32(b7, b6, b5, b4)) << 32) | ((int64_t)make_int32(b3, b2, b1, b0)))

#define byte7(val) ((byte)((val >> 56) & 0xff))

#define byte6(val) ((byte)((val >> 48) & 0xff))

#define byte5(val) ((byte)((val >> 40) & 0xff))

#define byte4(val) ((byte)((val >> 32) & 0xff))

#define byte3(val) ((byte)((val >> 24) & 0xff))

#define byte2(val) ((byte)((val >> 16) & 0xff))

#define byte1(val) ((byte)((val >> 8)  & 0xff))

#define byte0(val) ((byte)(val & 0xff))

//// ------------ byte -----------------------

//// ------------- LOGGER ----------------------
#ifndef LOG_STREAM_TRACE
#define LOG_STREAM_TRACE std::cout << "[TRACE] "
#endif

#ifndef LOG_STREAM_DEBUG 
#define LOG_STREAM_DEBUG std::cout << "[DEBUG] "
#endif

#ifndef LOG_STREAM_INFO
#define LOG_STREAM_INFO std::cout << "[INFO] "
#endif

#ifndef LOG_STREAM_WARN
#define LOG_STREAM_WARN std::cerr << "[WARN] "
#endif

#ifndef LOG_STREAM_ERROR
#define LOG_STREAM_ERROR std::cerr << "[ERROR] "
#endif

#ifndef LOG_STREAM_FATAL
#define LOG_STREAM_FATAL std::cerr << "[FATAL] "
#endif

#ifndef LOG_EOM
#define LOG_EOM std::endl
#endif

//// ------------- LOGGER ----------------------

//// ------------- typedef ---------------------

typedef char byte;
typedef unsigned char ubyte;

//// ------------- typedef ---------------------

#endif

